package com.team2.outpatient_sys.service.impl;

import com.team2.outpatient_sys.dao.DepartmentDao;
import com.team2.outpatient_sys.dao.DoctorDao;
import com.team2.outpatient_sys.pojo.Administrator;
import com.team2.outpatient_sys.pojo.Department;
import com.team2.outpatient_sys.pojo.Doctor;
import com.team2.outpatient_sys.pojo.User;
import com.team2.outpatient_sys.service.LoginService;
import com.team2.outpatient_sys.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private DoctorDao doctorDao;
    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public String login(Administrator administrator) {
        Authentication authentication = new UsernamePasswordAuthenticationToken("a:" + administrator.getUsername(), administrator.getPassword());
        authenticationManager.authenticate(authentication);


        HashMap<String, String> tokenInfo = new HashMap<>();
        tokenInfo.put("username", administrator.getUsername());
        tokenInfo.put("role", "admin");

        return JwtUtils.getToken(tokenInfo);
    }

    @Override
    public String login(Doctor doctor) {
        Authentication authentication = new UsernamePasswordAuthenticationToken("d:" + doctor.getUsername(), doctor.getPassword());
        authenticationManager.authenticate(authentication);

        HashMap<String, String> tokenInfo = new HashMap<>();
        tokenInfo.put("username", doctor.getUsername());
        String doctorRole = getDoctorRole(doctor.getUsername());
        tokenInfo.put("role", doctorRole);

        return JwtUtils.getToken(tokenInfo);
    }

    /**
     * 根据username获取医护人员角色名
     * 角色分为三类：药房、挂号、门诊
     * @param username 用于查询的医护人员的用户名
     * @return 医护人员角色名的字符串
     */
    private String getDoctorRole(String username) {
        Doctor doctor = doctorDao.queryByUsername(username);
        Department department = departmentDao.queryById(doctor.getDeptId());

        if ("药房".equals(department.getName()))
            return "pharmacy";
        else if ("挂号".equals(department.getName()))
            return "registration";
        else
            return "doctor";
    }

    @Override
    public String login(User user) {
        Authentication authentication = new UsernamePasswordAuthenticationToken("u:" + user.getUsername(), user.getPassword());
        authenticationManager.authenticate(authentication);

        HashMap<String, String> tokenInfo = new HashMap<>();
        tokenInfo.put("username", user.getUsername());
        tokenInfo.put("role", "patient");

        return JwtUtils.getToken(tokenInfo);
    }
}
